'use strict';
	angular.module('autoentry.entrySettingCGRK', ['app.services'])

	.controller('entrySettingCGRKCtrl',['$rootScope', '$scope', 'BizData', 'systemService', f])
	
    function f($rootScope, $scope, BizData, systemService) {
		
		var Model = this;
		
		var billType = "CGRK";
		var billTypeName = "采购进货单";
		
		
		Model.dataMasterAfterRefresh = function(event) {
			console.log('dataMasterAfterRefresh-->',event);
			if(event.source.getCount()==0){
				Model.add();
			}
		};
		
		/**
		name:bizData#onValueChanged
		description: <b>[回调型事件]</b>数据变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"column" : 关系,
			"rowIndex" : 行索引,
			"value" : 新值,
			"originalValue" : 旧值
		}
		</xmp>	
		*/
		
		//输入值变化，监视input的输入，ng-change
		Model.dataMasterValueChanged = function(event) {
			console.log('dataMasterValueChanged.event-->>',event);
		};
		
		
		var options = {};
		options.container = Model;
		options.concept = 'EntrySetting';
		options.name = 'dataMaster';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
		options.creater = '/autoentry/entrySetting/cgrk/create';
		options.reader = '/autoentry/entrySetting/cgrk/query';
		options.writer = '/autoentry/entrySetting/cgrk/save';
		options.deleter = '/autoentry/entrySetting/cgrk/delete';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = '';
		options.master = {"data":"","relation":""};
		options.directDeleteMode = false;
		//options.orderBy = '';
		options.limit = -1;
		
		options.onAfterRefresh = Model.dataMasterAfterRefresh;
		options.onValueChanged = Model.dataMasterValueChanged;

		Model.dataMaster = new BizData(options);

		//监视对象中的所有变动
		Model.dataMaster.deregRows = $scope.$watch('Model.dataMaster.rows',Model.dataMaster.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
		//////////////////////////////////////////////
		
		//设置过滤条件
		Model.dataMaster.filter = " EntrySetting.billType='" + billType + "' ";
		
		
		//新建
		Model.add = function(){
			
		     var options = {
			         index : 0,
			         defaultValues : [
			             {
			            	 billType: billType
			            	 ,billTypeName: billTypeName
			            	 ,gatherFlag: 0
			            	 ,samenoFlag: 0
			            	 ,debitSummary: 0
			            	 ,creditSummary: 0
			            	 
			            	 ,captionFrom1: 0
			            	 ,captionFrom2: 0
			            	 ,captionFrom3: 0
			            	 ,captionFrom4: 0
			            	 ,captionFrom5: 0
			            	 ,captionFrom6: 0
			            	 
							 ,userId: ':getCurrentPersonID()'
							 ,enterpriseNo: ':getCurrentOgnFID()'
							 ,enterpriseName: ':getCurrentOgnLongName()'
							 ,version: 0

			            }//,
//			             {itemNo:'a2',itemDescribe:'b2'}
			         ]
			    };    

			Model.dataMaster.newData(options);
		};
		
		//修改
		Model.edit = function(){
			Model.dataMaster.editData();
		};
		
		//保存
		Model.save = function() {
			Model.dataMaster.saveData();
		};

		//取消编辑
		Model.saveCancel = function() {
			Model.dataMaster.editCancel();
		};
	    
		//刷新数据
		Model.refresh = function(){
			Model.dataMaster.refreshData();
		};
		
		//打开对话框（选择会计科目）
		Model.triggerCaptionNoClick = function(columnNo,columnName){
			var dataMaster = Model.dataMaster;
			var rowID = dataMaster.getCurrentID();
			if(dataMaster.getValue(columnNo, rowID)){
	         	dataMaster.setValue(columnNo, null, rowID);
	         	dataMaster.setValue(columnName, null, rowID);
	         	return;
			}
			
			var options = {};
			options.recordState = 'none';
			options.id = dataMaster.getValue('enterpriseNo',rowID);

			var modalInstance = systemService.openCaptionNoDialog(options);

	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (event) {  
	             //成功关闭窗口
	         	var returnData = event.data;

	         	dataMaster.setValue(columnNo, returnData[0].captionNo, rowID);
	         	dataMaster.setValue(columnName, returnData[0].captionName, rowID);

	        }, function (reason) {  
	            console.info('Modal dismissed at: ' + new Date());  
	        });  
		};
		
		
		//装载缺省数据
		/*
	    Model.loadData = function(){

	    	var options = {};
	    	options.billType = $scope.billType;
	    	
			$.ajax({
	            type: "POST",
	            async : true,//异步（默认），同步为false
	            url: $rootScope.getProjectName() + "/autoentry/entrySetting/loadData",
	            data: JSON.stringify(options),
	            contentType: "application/json; charset=utf-8",
	            dataType: "json",
	            success: function (data) {
	                // Play with returned data in JSON format
					console.log('data->',data);
					if(data.code==0){
						Model.refresh();
						$scope.$apply();
					}else{
						alert(data.info);
					}
	            },
	            error: function (msg) {
	                alert('ajax调用错误信息：'+msg.responseText);
	            }
	        });
	    };
		*/
		
	    //凭证类别
	    $scope.dataPzBillType = [
            {value:'YBPZ',label:'一般凭证'}
            ,{value:'XSPZ',label:'现收凭证'}
            ,{value:'XZPZ',label:'现支凭证'}
            ,{value:'ZZPZ',label:'转账凭证'}
            ];
		//生成凭证方式
		$scope.dataGatherFlag = [{"value":0,"label":'逐张'},{"value":1,"label":'汇总'}];
		
	    //参数来源类型
		//存货
	    $scope.dataCaptionFrom1 = [{value:0,label:'-- 选择 --'},{value:1,label:'1.品号存货会计科目'},{value:2,label:'2.分录设置'}];
	    //应交增值税
	    $scope.dataCaptionFrom2 = [{value:0,label:'-- 选择 --'},{value:1,label:'1.应付系统参数设置'},{value:2,label:'2.分录设置'}];
	    //账款
	    $scope.dataCaptionFrom3 = [{value:0,label:'-- 选择 --'},{value:1,label:'1.供应商账款科目'},{value:2,label:'2.分录设置'}];
	    //费用
	    $scope.dataCaptionFrom4 = [{value:0,label:'-- 选择 --'},{value:1,label:'1.分录设置'}];
	    //预付
	    $scope.dataCaptionFrom5 = [{value:0,label:'-- 选择 --'},{value:1,label:'1.预付账款会计科目来源'},{value:2,label:'2.分录设置'}];
	    //差异
	    $scope.dataCaptionFrom6 = [{value:0,label:'-- 选择 --'},{value:1,label:'1.分录设置'}];
		
		
		Model.refresh();

		////////////////////启动执行结束///////////////////////////////////////////////////
		
	    return Model;
		
	}	
	
